var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var page = 1;
var loadingMore = false;
var hasMore = true;
var screnn_w = 320;

Page({

   data: {

     uid:0,
     token: '',
     waithao: 0,
     content_item_w: 100,
     goodTypeView_w: 100,
     colorSet: '',
     myhao:'',
     postuid:'',
     numberV:'',
     tc:1,

     verified:false, //是否已通过咨客
   },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tc = app.get_tc();
    var that = this;
    that.setData({
      tc: tc,
    });

    var titlename = tc == 1 ? '取號' : '取号';
    wx.setNavigationBarTitle({
      title: titlename,
    })

    hasMore = true;
    page = 1;


    that.setData({
      postuid: options.orgid
    });
    that.getLoginUserInfo();
    that.getWaithao();
    that.getMyhao();
    that.getverified();

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
    });

    wx.getSystemInfo({
      success: function (res) {
    
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screnn_w : res.windowWidth,
          content_w: res.windowWidth - 78,
          goodTypeView_w: (res.windowWidth - 78) / 3
        });
        
      }
    })

    //获取颜色配置
    app.getColorSetData(function (color) {
      var col = color.goods_col ? parseInt(color.goods_col) : 3;
      col = col > 0 ? col : 3;
      that.setData({
        colorSet: color,
        content_item_w: (screnn_w - (col + 1) * 5) / col,
        goodTypeView_w: screnn_w / 3
      })
    });
  },

  //判断用户是否是该门店咨客
  getverified:function(){
    var that = this;
     util.api({
      method: "get",
      data: {
        ac: "getverified",
        uid: that.data.uid,
        mendianId: that.data.postuid,
      },
      
      success: function (responseData) {
        console.log("getverified:", responseData.data);
        if(responseData.data.res == true){
          that.setData({
            verified:true
          })
        }else{
          that.setData({
            verified:false
          })
        }
        
      }
    });
  },

  getLoginUserInfo: function(inviteuid){
    var that = this;
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function (res) {
      if (res && res != -1) {
        //更新数据
        that.setData({
          uid: res.uid,
          token: res.token
        })
      } else {
        app.reAuthorize(reGetUserInfo);
      }
    }, inviteuid);
  },
  
  //查询领号等待信息
  getWaithao:function(){
    var that = this;
    //查询领号等待数
    var privatekey = utilMd5.hexMD5(that.data.uid + 'waithao' + that.data.token);
    console.log("that.data.uid:"+that.data.uid+"that.data.token:"+that.data.token+"util.orgid:"+util.orgid);
     util.api({
      method: "post",
      data: {
        ac: "waithao",
        uid: that.data.uid,
        orgid: that.data.postuid, 
        privatekey: privatekey
      },
      
      success: function (responseData) {
        console.log("调试--查询领号等待信息接口:", responseData.data);
        that.setData({
          waithao: responseData.data,
        });
        
      }
    });
  },
  
  makeCallPhone: function (e) {
    var phoneNum = e.currentTarget.dataset.number;
    console.log("phoneNum:" + phoneNum);
    wx.makePhoneCall({
      phoneNumber: phoneNum,
      success: function (e) {
        console.log("make call success:", JSON.stringify(e));
      },
      fail: function (e) {
        console.log("make call fail:", JSON.stringify(e));
      }
    })
  },

  //我的排号信息
  getMyhao:function(){
    
    var that = this;
     util.api({
      method: "get",
      data: {
        ac: "getmyhao",
        uid: that.data.uid,
        orgid: that.data.postuid
      },
      
      success: function (responseData) {
        console.log("调试--我的排号信息接口:", responseData.data);
        console.log("调试--我的排号号码:", responseData.data.number);
        var resultData = responseData.data;
        that.setData({
          myhao:resultData
        })
      }
    });
  },
   //领号接口
   getBookNo:function(){
    var that = this;
    var privatekey = utilMd5.hexMD5(that.data.uid + 'linghao' + that.data.token);
     util.api({
      method: "post",
      data: {
        ac: "linghao",
        uid: that.data.uid,
        orgid: that.data.postuid, 
        privatekey: privatekey,
        userNum:that.data.numberV
      },
      
      success: function (responseData) {
        console.log("调试--领号接口接口:", responseData.data);
        var contentname = this.data.tc == 1 ? '已成功加取號' : '已成功取号';
        wx.showToast({
          title: contentname,
        })

        that.getWaithao();
        that.getMyhao();
      }
    });
  },


  //刷新当前页
  refreshPage: function() {
      wx.redirectTo({
          url:'/pages/book/book'
      })
  },


  //点击输入人数
  numberInput:function(e){
    this.setData({
      numberV: e.detail.value
    })
  },

})